package com.koron.common.task.web.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * User: pan
 * Date: 2019/9/12
 * Description: No Description
 */
@Slf4j
public class HttpClient {

    private RestTemplate template = null;

    private String token = null;

    public HttpClient(String token) {
        Assert.hasText(token, "token不能为空");
        this.token = token;
        this.template = new RestTemplate();
        List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
        interceptors.add((request, bytes, execution) -> {
            request.getHeaders().set("token", token);
            return execution.execute(request, bytes);
        });
        template.setInterceptors(interceptors);
    }

    public String doGet(String url, Map<String, String> params) {
        if(params == null) {
            return null;
        }
        if(params.size() == 0) {
            return doGet(url);
        }
        StringBuffer sb = new StringBuffer(url).append("?");
        Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> entry = iterator.next();
            sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
        }
        url = sb.substring(0, sb.length() - 1);
        return doGet(url);
    }


    public String doGet(String url) {
        return template.getForObject(url, String.class);
    }

    /**
     * post请求(body)
     * @param url
     * @param body
     * @return
     */
    public String doPost(String url, String body) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<String> request = new HttpEntity<>(body, headers);
        return template.postForObject(url, request, String.class);
    }

    /**
     * post请求(params)
     * @param url
     * @param params
     * @return
     */
    public String doPost(String url, Map<String, String> params) {
        Assert.isTrue(params != null && params.size() > 0, "参数不合法");
        HttpHeaders headers = new HttpHeaders();
        HttpMethod method = HttpMethod.POST;
        // 以表单的方式提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        MultiValueMap<String, String> queryMap = new LinkedMultiValueMap<>();
        Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> entry = iterator.next();
            queryMap.add(entry.getKey(), entry.getValue());
        }
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(queryMap, headers);
        return template.exchange(url, method, request, String.class).getBody();
    }
}
